package com.example.myuser.controller;


import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.example.myorder.entity.Order;
import com.example.myuser.entity.User;
import com.example.myuser.feign.OrderFeignService;
import com.example.myuser.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author li
 * @since 2024-11-05
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @Autowired
    private OrderFeignService orderFeignService;

    @RequestMapping("/queryUserById")
    @SentinelResource(value = "getUser",blockHandler = "handleException")
    public User queryUserById(Integer id){
        return userService.getById(id);
    }

    @RequestMapping("/queryUserVOById")
    public User queryUserVOById(Integer id){
        User user = userService.getById(id);
        if (Objects.isNull(user)){
            return user;
        }
        List<Order> orderList = orderFeignService.getOrderListByUserId(id);
        user.setOrderList(orderList);
        return user;
    }


    public User handleException(Integer id, BlockException ex) {
        User userEntity = new User();
        userEntity.setName("===被限流降级啦===");
        return userEntity;
    }
}
